package com.nuanshui.heatedloan.config;

import com.nuanshui.framework.springContext.Environment;
import com.nuanshui.heatedloan.extension.filter.SessionFilter;
import com.nuanshui.heatedloan.extension.filter.TraceFilter;
import com.nuanshui.heatedloan.middleware.config.MiddlewareConfig;
import com.nuanshui.heatedloan.module.config.ModuleConfig;
import com.nuanshui.heatedloan.service.config.DubboConfig;
import com.nuanshui.heatedloan.service.config.ServiceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.io.IOException;
import java.net.URL;

public class WebAppInitializer extends
		AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected String[] getServletMappings() {
		
		return new String[] { "/" };
	}
	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class<?>[] {ApplicationConfig.class,
				DubboConfig.class,
				ServiceConfig.class,
				SecurityConfig.class,
				MiddlewareConfig.class,
				ModuleConfig.class};
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[] { WebMvcConfig.class };
	}

	/**
	 * 获取过滤器
	 * @param 
	 * @author guohao
	 * @date 2015年5月5日 下午2:02:05
	 */
	@Override
	protected Filter[] getServletFilters() {
		//utf-8编码过滤器
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		characterEncodingFilter.setForceEncoding(true);
        //spring security 过滤器
		DelegatingFilterProxy securityFilterChain = new DelegatingFilterProxy(
				"springSecurityFilterChain");
        //用户访问过滤器
		TraceFilter traceFilter = new TraceFilter();

		return new Filter[] {new SessionFilter(),characterEncodingFilter, securityFilterChain,
			 traceFilter};
	}
	
	@Override
	protected void registerContextLoaderListener(ServletContext servletContext) {
		// TODO Auto-generated method stub
		super.registerContextLoaderListener(servletContext);
		//servletContext.addListener(new CustomedContextLoaderListener());
	}
	


	@Override
	protected void customizeRegistration(
			ServletRegistration.Dynamic registration) {
		registration.setInitParameter("defaultHtmlEscape", "true");
	}

	static class ConfigApplicationContextInitializer implements
			ApplicationContextInitializer<ConfigurableApplicationContext> {

		private Logger LOG = LoggerFactory
				.getLogger(ConfigApplicationContextInitializer.class);

		@Override
		public void initialize(ConfigurableApplicationContext applicationContext) {
			com.nuanshui.heatedloan.dubbo.extension.container.Main.parseEnv();
			Environment.setRestEnv();
		}
	}
	
	
	public static void main(String[] args) {
	  

	}
	
}